import { http } from '../../utils/request'
import type {
  ChatConversationResponse,
  ChatMessageResponse,
  StreamRequest,
} from './type'
import type { Response } from '../type'

export const conversationList = async (
  userId: string,
  days: number = 7
): Promise<Response<ChatConversationResponse[]>> => {
  return await http.get(`/ai-api/agent/conversation/list/${userId}/${days}`)
}

export const messageList = async (
  threadId: string
): Promise<Response<ChatMessageResponse[]>> => {
  return await http.get(`/ai-api/agent/message/list/${threadId}`)
}

export const stream = async (data: StreamRequest) => {
  const token = localStorage.getItem('authorization')
  const headers: { [key: string]: string } = {
    'Content-Type': 'application/json',
  }
  if (token) {
    headers['Authorization'] = `Bearer ${token}`
  }
  return await fetch('/ai-api/agent/stream', {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(data),
  })
}
